package jt.classic.app.dms.init;

import jt.classic.app.dms.timer.DMSFtpFileSaveTimer;
import jt.classic.app.dms.timer.DMSFtpFolderTimer;
import jt.classic.app.dms.timer.DMSUserFolderTimer;

import jt.classic.system.ISystemException;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.database.SystemDataSourceException;
import jt.classic.system.init.AbstractInitializtion;
import jt.classic.system.lic.params.SignParamsGetter;
import jt.classic.system.lic.util.LicVerify;

import org.limp.basework.console.XSystemConfig;

import java.io.File;

import java.sql.Connection;


/**
 * DMS 系统初始化
 *
 * @author mido
 *
 */
public class DMSBasic100Initializtion extends AbstractInitializtion {
    public void load() throws ISystemException {
        SignParamsGetter spg = SignParamsGetter.getInstance();

        spg.load();

        LicVerify lv = new LicVerify();

        Connection conn = null;

        try {
            if (spg.getUserSize() > -1) {
                SystemDataSource ds = SystemDataSource.getInstance();

                conn = ds.getConnection();
            }

            lv.verify("dms100.lic", spg, conn);
        } catch (SystemDataSourceException ex) {
            throw new ISystemException("datasource is invalid, can't check dms-lic",
                ex);
        } catch (ISystemException ex) {
            System.err.println("{========== dms license is invalid ==========}");
            System.err.println("{========== " + spg.getParamsText() +
                "==========}");
            throw new ISystemException(spg.getParamsText(), ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        String userhome = (String) XSystemConfig.config()
                                                .getProperty("XSYSTEM_USER_HOME");
        File file = new File(userhome);

        if (!file.exists()) {
            if (!file.mkdirs()) {
                throw new ISystemException("DMS initialize user-home('" +
                    userhome + "') error.");
            }
        }

        DMSUserFolderTimer timer = new DMSUserFolderTimer(file);
        timer.runTask();

        DMSFtpFolderTimer timer1 = new DMSFtpFolderTimer(file);
        timer1.runTask();

        DMSFtpFileSaveTimer timer2 = new DMSFtpFileSaveTimer();
        timer2.runTask();
    }
}
